from selenium import webdriver
from time import sleep
import csv
#

def writecsv(item):
    with open('百姓网南京招工.csv', 'a', encoding='utf-8', newline='')as f:
        wir = csv.writer(f)
        try:
            wir.writerow(item)
        except:
            print('error')


driver = webdriver.Chrome(r'E:\Google\chromedriver.exe')


#  联系人
def contacts():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[8]/label[2]')
        return a5.text
    except:
        return ''


def contacts2():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[7]/label[2]')
        return a5.text
    except:
        return ''


def contacts3():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[2]/label[2]')
        return a5.text
    except:
        return ''


def contacts4():
    try:
        a5 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[3]/label[2]')
        return a5.text
    except:
        return ''


#  招工信息
def getInfo():
    try:
        a3 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/section/div[3]')
        return a3.text
    except:
        return ''


def getInfo2():
    try:
        a3 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/section/div[2]')
        return a3.text
    except:
        return ''


def getInfo3():
    try:
        a3 = driver.find_element_by_xpath('//*[@id="agent-company-tab-1"]/div[2]')
        return a3.text
    except:
        return ''


def getInfo4():
    try:
        a3 = driver.find_element_by_xpath('//*[@id="agent-company-tab-1"]/div[1]/p')
        return a3.text
    except:
        return ''


#  地址
def address():
    try:
        a9 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[2]/span')
        return a9.text
    except:
        return ''


def address2():
    try:
        a9 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div/div[2]/div[1]/span')
        return a9.text
    except:
        return ''



def getElement():
    driver.get('https://nanjing.baixing.com/gongren/')
    sleep(3)
    w = 300
    for i in range(2, 116):
        try:
            driver.get('https://nanjing.baixing.com/gongren/')
            sleep(3)
            driver.execute_script('window.scrollTo(0,' + str(w) + ');')
            w = w + 125
            sleep(5)
            driver.find_element_by_xpath('//*[@id="all-list"]/ul/li[' + str(i) + ']/div[1]/div/a[1]').click()
            windows = driver.window_handles
            driver.switch_to.window(windows[-1])
            a1 = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/div[1]/h1')  # 招工标题
            a2 = driver.find_element_by_xpath('/html/body/div[1]/ul/li[4]/a')  # 工种
            # 招工信息
            a3 = getInfo()
            ans = ''
            a3Text1 = getInfo()
            a3Text2 = getInfo2()
            a3Text3 = getInfo3()
            a3Text4 = getInfo4()
            if a3Text1 != '':
                ans = a3Text1
            elif a3Text2 != '':
                ans = a3Text2
            elif a3Text3 != '':
                ans = a3Text3
            elif a3Text4 != '':
                ans = a3Text4
            else:
                ans = "四个都没拿到"

            # 时间
            time = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div/span[2]')
            """/html/body/div[1]/div[1]/div[1]/div[1]/div[2]/div/span[2]"""
            time_text = time.text
            time_list = time_text.split('日')
            ti = time_list[0]
            # 联系人
            a5 = contacts2()
            ans3 = ''
            a5text1 = contacts()
            a5text2 = contacts2()
            a5text3 = contacts3()
            a5text4 = contacts4()
            if a5text2 != '':
                ans3 = contacts2()
            elif a5text1 != '':
                ans3 = contacts()
            elif a5text3 != '':
                ans3 = contacts3()
            elif a5text4 != '':
                ans3 = contacts4()
            else:
                ans3 = "四个都没拿到"

            # 点击查看联系电话
            driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div[1]/div[2]/div/section/ul/li[1]/a[2]').click()
            sleep(1)

            # 点击关闭
            driver.find_element_by_xpath(
                '//*[@id="fillresume"]/div/div/div[1]/button').click()
            sleep(1)

            # 联系电话
            a8 = driver.find_element_by_xpath(
                '/html/body/div[1]/div[1]/div[1]/div[2]/div/section/ul/li[1]/a[1]')

            # 工作地址
            a9 = address()
            ans2 = ''
            a9text1 = address()
            a9text2 = address2()
            if a9text1 != '':
                ans2 = address()
            elif a9text2 != '':
                ans2 = address2()
            else:
                ans2 = "两个都没拿到"

            sheng = '江苏'  # 省
            shi = '南京'  # 市

            list = [a1.text, a2.text, ans, ti, ans3, a8.text, sheng, shi, ans2]

            print(list)
            writecsv(list)
        except:
            print('NoSuchElementException')


if __name__ == "__main__":
    getElement()
